package com.westos.echufang.controller;

import com.westos.echufang.constants.DefaultRegex;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.regex.Pattern;

@Controller
@ResponseBody
public class AjaxCheckController {

    //手机号格式校验
    @RequestMapping("/phoneCheck")
    public String phoneCheck(String phone) {
        phone = phone.trim();
        String phoneMsg;
        if (phone.length() == 0) phoneMsg = "请输入手机号！";
        else if (!Pattern.matches(DefaultRegex.PHONE_REGEX, phone)) phoneMsg = "请输入正确的手机号！";
        else phoneMsg = "";
        return phoneMsg;
    }

    //登录密码格式校验
    @RequestMapping("/pwdCheck")
    public String pwdCheck(String password) {
        String pwdMsg;
        if (password.length() == 0) pwdMsg = "你还没有输入密码！";
        else pwdMsg = "";
        return pwdMsg;
    }

    //验证码格式校验
    @RequestMapping("/codeCheck")
    public String codeTest(String code) {
        code = code.trim();
        String codeMsg;
        if (code.length() == 0) codeMsg = "请输入验证码";
        else if (code.length() != 8) codeMsg = "验证码格式错误";
        else codeMsg = "";
        return codeMsg;
    }


    //注册密码8-16位 不包含空格
    @RequestMapping("/regPwdCheck")
    public String regPwdCheck(String regPwd) {
        String regMsg;
        if (regPwd.length() == 0) regMsg = "请输入密码！";
        else if (!Pattern.matches(DefaultRegex.PASSWORD_REGEX, regPwd)) regMsg = "密码格式错误！";
        else regMsg = "";
        return regMsg;
    }

    //职业
    @RequestMapping("/jobCheck")
    public String jobCheck(String job) {
        String jobMsg;
        if (job.equals("select")) jobMsg = "请选择职业！";
        else jobMsg = "";
        return jobMsg;
    }

    //文件格式
    @RequestMapping("/fileCheck")
    public String fileCheck(String fileName, String job) {
        System.out.println(fileName);
        System.out.println(job);
        String fileRealName=fileName.substring(0,fileName.indexOf('.'));
        System.out.println(fileRealName);
        String fileFormat = fileName.substring(fileName.indexOf('.') + 1);
        System.out.println(fileFormat);
        String fileMsg;
        if (fileName.length() == 0) {
            fileMsg = "请上传资格证照片！";
        } else if (!(job.equals("doctor")&&Pattern.matches(DefaultRegex.DOCTOR_REGEX,fileRealName))&&!(job.equals("pharmacist")&&Pattern.matches(DefaultRegex.PHARMACIST_REGEX,fileRealName))){
            System.out.println(!(job.equals("doctor")&&Pattern.matches(DefaultRegex.DOCTOR_REGEX,fileRealName))&&!(job.equals("pharmacist")&&Pattern.matches(DefaultRegex.PHARMACIST_REGEX,fileRealName)));
            fileMsg=  "照片名格式错误！";
        }
        else if (!fileFormat.equals("jpg") && !fileFormat.equals("png")) {
            fileMsg = "照片格式错误！";
        } else fileMsg = "";
        return fileMsg;
    }

    //姓名
    @RequestMapping("/nameCheck")
    public String nameCheck(String name) {
        name = name.trim();
        String nameMsg;
        if (name.length() == 0) nameMsg = "请输入姓名！";
        else if (!Pattern.matches(DefaultRegex.NAME_REGEX, name)) nameMsg = "请输入正确的姓名！";
        else nameMsg = "";
        return nameMsg;
    }

    //邮箱
    @RequestMapping("emailCheck")
    public String emailRegex(String email) {
        email = email.trim();
        String emailMsg;
        if (email.length() == 0) emailMsg = "请输入邮箱！";
        else if (!Pattern.matches(DefaultRegex.EMAIL_REGEX, email)) emailMsg = "请输入正确的邮箱！";
        else emailMsg = "";
        System.out.println(Pattern.matches(DefaultRegex.EMAIL_REGEX, email));
        return emailMsg;
    }
}
